<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #result {
            width: 400px;
            height: 200px;
            border: solid 2px #14579e;
        }
    </style>
</head>
<body>
<button id="button_01">点击发送请求</button>
<button id="button_02">点击取消请求</button>
<script>
    const button_01 = document.getElementById("button_01")
    const button_02 = document.getElementById("button_02")

    let xhr = null
    let isSending = false

    button_01.onclick = () => {
        if (isSending) {
            xhr.abort()
        }

        isSending = true

        // 1.创建对象
        xhr = new XMLHttpRequest()

        // 2.初始化设置请求方法和URL
        xhr.open("GET", "http://127.0.0.1:8000/delay")

        // 3.发送
        xhr.send()

        xhr.onreadystatechange = () => {
            if (xhr.readyState === 4) {
                isSending = false
            }
        }
    }

    button_02.onclick = () => {
        // 取消请求
        xhr.abort()
    }
</script>
</body>
</html>